package com.example.cargetter.socket;

import com.example.cargetter.config.UdpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/udp")
public class UdpClient {

    private final static Logger logger = LoggerFactory.getLogger(UdpClient.class);

    @PostMapping("/send")
    public String send(@RequestParam("message") byte[] message) {
        UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("192.168.31.189", UdpConfig.getSendingPort());
        logger.info("发送UDP信息: {" + message + "}");
        handler.handleMessage(MessageBuilder.withPayload(message).build());
        logger.info("发送成功");
        return "  ";
    }
}